import Taro from "@tarojs/taro";
import { Middleware, registerMiddleware, Router } from "tarojs-router-next";
import { useUserStore } from "@/store/user";

export const M1: Middleware = async (ctx, next) => {
  console.log("第一个中间件执行：", ctx.route.url);
  const user = useUserStore();
  if (!user.token) {
    const { confirm } = await Taro.showModal({
      title: "提示",
      content: "尚未登录",
    });

    if (confirm) Router.toLogin();

    // 直接返回，不执行 next 即可打断中间件向下执行
    return;
  }

  await next();
};

// registerMiddleware(M1);
